<template>
	<div class="sys-pos-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" class="fl">
				<el-form-item label="库位编号" prop="storageNum"
					:rules="[{ required: true, message: '库位编号不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.storageNum" placeholder="库位编号" clearable class="input" />
				</el-form-item>
				<el-form-item label="库位名称" prop="storageName"
					:rules="[{ required: true, message: '库位编号不能为空', trigger: 'blur' }]">
					<el-input v-model="state.ruleForm.storageName" placeholder="库位名称" clearable class="input" />
				</el-form-item>
				<el-form-item label="所属仓库" :rules="[{ required: true, message: '所属仓库不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.whNum" filterable clearable class="w100"
						@change="handleChange(state.ruleForm.whNum)">
						<el-option v-for="item in state.belongWarehouses" :key="item"
							:label="item.eg_wms_warehouse.WHName" :value="item.eg_wms_warehouse.WHNum" />
					</el-select>
				</el-form-item>
				<el-form-item label="所属区域" prop="regionNum"
					:rules="[{ required: true, message: '所属区域不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.regionNum" filterable clearable class="w100">
						<el-option v-for="item in state.belongAreas" :key="item" :label="item.RegionName"
							:value="item.RegionNum" />
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="库位类别" prop="storageType"
					:rules="[{ required: true, message: '库位类别不能为空', trigger: 'blur' }]">
					<el-select v-model="state.ruleForm.storageType" filterable clearable class="w100">
						<el-option v-for="item in state.belongType" :key="item.name" :label="item.label"
							:value="item.name" />
					</el-select>
				</el-form-item> -->
				<el-form-item label="组号" prop="storageGroup" v-if="state.ruleForm.storageType == 0">
					<el-input v-model="state.ruleForm.storageGroup" placeholder="组号" clearable class="input" />
				</el-form-item>
				<el-form-item label="巷道号" prop="roadwayNum" v-if="state.ruleForm.storageType == 1">
					<el-input v-model="state.ruleForm.roadwayNum" placeholder="巷道号" clearable class="input" />
				</el-form-item>
				<el-form-item label="货架号" prop="shelfNum" v-if="state.ruleForm.storageType == 1">
					<el-input v-model="state.ruleForm.shelfNum" placeholder="货架号" clearable class="input" />
				</el-form-item>
				<el-form-item label="层号" prop="floorNumber" v-if="state.ruleForm.storageType == 1">
					<el-input v-model="state.ruleForm.floorNumber" placeholder="层号" clearable class="input" />
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="state.ruleForm.storageStatus" inline-prompt active-text="启用" inactive-text="禁用"
						:active-value="0" :inactive-value="1" />
				</el-form-item>
				<el-form-item label="是否等待点库位">
					<el-switch v-model="state.ruleForm.storageHoldingPoint" inline-prompt active-text="是" inactive-text="否"
						:active-value="1" :inactive-value="0" />
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea" v-model="state.ruleForm.storageRemake" placeholder="请输入备注内容"></el-input>
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditPos">
import { onMounted, reactive, ref } from 'vue';

import { feature, getAPI } from '/@/utils/axios-utils';
import { APIJSONApi, SysDictDataApi, SysPosApi } from '/@/api-services/api';
import { WmsApi } from '/@/api/wh';
import { ElMessage } from 'element-plus';

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {

	},
	belongWarehouses: [],
});
onMounted(async() => {

		try {
			const res = await feature(
        getAPI(APIJSONApi).apiAPIJSONGetPost({
          "[]": {
            "eg_wms_warehouse": {
              IsDelete: 0,
              "@column": "WHName,WHNum",
            },
          },
        })
      );

  
      state.belongWarehouses = res[1].data.result["[]"];
		} catch (error) {
			console.log(error);
		}
	
})
const handleChange = async (id) => {
	state.ruleForm.regionNum = ''
	state.belongAreas = [];//arr
	try {
		const res = await feature(getAPI(APIJSONApi).apiAPIJSONGetPost(
			{
				"[]": {
					"eg_wms_region": {
						"IsDelete": 0,
						"WHNum": id,
						"@column": "RegionNum,RegionName"
					}
				}
			}
		));
		res[1].data.result['[]'].forEach((item, index, input) => {
			input[index] = item.eg_wms_region
		})
		console.log(state.belongAreas);
		state.belongAreas = res[1].data.result['[]'];
	} catch (error) {
		console.log(error);
	}
}
// 打开弹窗
const openDialog = async (row: any) => {
	var res = await getAPI(SysDictDataApi).apiSysDictDataDataListCodeGet('w_type');
	state.wTypes = res.data.result;
	state.ruleForm = JSON.parse(JSON.stringify(row));
	console.log(state.ruleForm.whStatus);

	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;


};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		console.log(state.ruleForm.id);

		if (state.ruleForm.id) {
			console.log(state.ruleForm);

			await WmsApi().eGStoragePagePut(state.ruleForm)
				.then(function (res) {
					ElMessage.success('修改成功');


					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});
		} else {
			
			await WmsApi().eGStoragePageAdd(state.ruleForm)
				.then(function (res) {
					ElMessage.success('新增成功');

					closeDialog();
				})
				.catch(function (error) {
					console.log(error);
				});

		}
	});
}

// 导出对象
defineExpose({ openDialog });
</script>
<style lang="scss" scoped>
.fl {
	width: 70%;
	margin: 20px auto;
}
</style>